#include"common_headers.h"
void createvfs31(char label[30], int size)
{
    int i=0, is_file_already_exist, is_valid_file_label, is_valid_size;
	FILE *fptr;
    fptr=NULL;
        
    if(strlen(label)<=30)
    {
    	is_valid_file_label=validateFileName(label);    
		if(is_valid_file_label)
		{
			is_valid_size=validateSize(size);    	
			if(is_valid_size)
			{
				is_file_already_exist = validateFileExistance(label);
				if(is_file_already_exist==0)
				{	
					strcpy(mainHeader.filelabel,label);
					mainHeader.maximumFileDescriptor=size;
					mainHeader.usedFileDescriptors=0;
					
					freelist=(char *)malloc(mainHeader.maximumFileDescriptor*sizeof(char));
					fileDescriptors=(struct FileDescriptor *)malloc(mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor));
					
					while(i<size)
					{
						freelist[i]=0;
					    fileDescriptors[i].locationblocknumber=i;
						i++;
					}
					
					fptr=fopen(label, "wb"); //open file in binary write mode
					if(fptr!=NULL)
					{
						//WRITE DETAIS TO FILE
					    fwrite(&mainHeader,sizeof(struct MainHeader),1,fptr);
						fwrite(freelist,(mainHeader.maximumFileDescriptor*sizeof(char)),1,fptr);
						fwrite(fileDescriptors,(mainHeader.maximumFileDescriptor*sizeof(struct FileDescriptor)),1,fptr);
						
						fseek(fptr,(mainHeader.maximumFileDescriptor*sizeof(struct Block)),SEEK_CUR);//reaching at the end of size
						fputc(EOF,fptr);
						fclose(fptr);
						free(freelist);
						free(fileDescriptors);
						mountstate=0;
						printf("createvfs_SUCCESS\n");
					}
					else
					{
						//ERR_VFS_CREATE_02 "CANNOT_CREATE_DATAFILE"    
						printf("createvfs_FAILURE %s\n", ERR_VFS_CREATE_02);     					
					}    	   			
				}
				else
				{
					//DATA_FILE_ALREADY_EXIST
					printf("createvfs_FAILURE %s\n", ERR_VFS_CREATE_01);     	
				}    		
			}
			else
			{
				//INVALID_SIZE
				printf("createvfs_FAILURE %s\n", ERR_VFS_CREATE_05);     	   		
			}    	
		}
		else
		{
			//INVALID_CHARACTER_IN_NAME
			printf("createvfs_FAILURE %s\n", ERR_VFS_CREATE_04);     	
		}
	}
    else
    {
    	//VFS_LABEL_TOO_LARGE
    	printf("createvfs_FAILURE %s\n", ERR_VFS_CREATE_07);     	
    }
        
}


